package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.tanhua.dubbo.pojo.RecommendUser;
import com.tanhua.dubbo.vo.PageInfo;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class TodayBestService {

    @Autowired
    private UserService userService;

    @Autowired
    private RecommendUserService recommendUserService;

    @Autowired
    private UserInfoService userInfoService;


    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;

    /**
     * 查询今日佳人
     *
     * @param token
     * @return
     */
    public TodayBest queryTodayBest(String token) {
        //查询当前的登录信息
        User user = userService.queryUserByToken(token);
        if (user == null) {
            return null;
        }
        //查询今日佳人
        TodayBest todayBest = recommendUserService.queryTodayBest(user.getId()); // id,得分
        if (todayBest == null) {
            // 默认推荐用户
            todayBest = new TodayBest();
            todayBest.setId(defaultUser);
        }
        //补全信息
        UserInfo userInfo = this.userInfoService.findById(todayBest.getId());
        todayBest.setAge(userInfo.getAge());
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setGender(userInfo.getSex().toString());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setTags(StringUtils.split(userInfo.getTags(), ","));
        return todayBest;
    }

    /**
     * 查询推荐用户列表
     *
     * @param queryParam
     * @param token
     * @return
     */
    public PageResult queryPageResult(RecommendUserQueryParam queryParam, String token) {
        // 查询当前登录的用户信息
        User user = userService.queryUserByToken(token);
        if (user == null) {
            return null;
        }
        // 根据用户id进行分页查询
        PageInfo<RecommendUser> pageInfo = recommendUserService.queryRecommendUserList(user.getId(), queryParam.getPage(), queryParam.getPagesize());

        // 推荐用户列表
        List<RecommendUser> records = pageInfo.getRecords();
        // 封装推荐用户的id
        List<Long> ids = new ArrayList<>();
        for (RecommendUser recommendUser : records) {
            ids.add(recommendUser.getUserId());
        }

        // 构造查询用户信息的条件
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", ids);

        if (StringUtils.isNotEmpty(queryParam.getGender())) { //性别条件
            if (queryParam.getGender().equals("man")) {
                queryWrapper.eq("sex", 1);
            } else {
                queryWrapper.eq("sex", 2);
            }
        }

        if (StringUtils.isNotEmpty(queryParam.getCity())) { //居住城市
            queryWrapper.eq("city", queryParam.getCity());
        }

        if (queryParam.getAge() != null) { //年龄
            queryWrapper.lt("age", queryParam.getAge());
        }

        List<UserInfo> userInfos = userInfoService.queryList(queryWrapper);

        // 封装完善TodayBest信息
        List<TodayBest> todayBestList = new ArrayList<>();
        for (UserInfo userInfo : userInfos) {
            TodayBest todayBest = new TodayBest();
            todayBest.setId(userInfo.getUserId());
            todayBest.setAge(userInfo.getAge());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setGender(userInfo.getSex().toString());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(), ","));
            // 设置缘分值
            for (RecommendUser recommendUser : records) {
                if (todayBest.getId().longValue() == recommendUser.getUserId().longValue()) {
                    double score = Math.floor(recommendUser.getScore()); //取整,98.2 -> 98
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }
            todayBestList.add(todayBest);
        }

        // 对推荐用户根据缘分值进行排序
        todayBestList.sort((o1, o2) -> (int) (o2.getFateValue() - o1.getFateValue()));

        return new PageResult(0, queryParam.getPagesize(), 0, queryParam.getPage(), todayBestList);
    }
}
